package com.ycsj.goldmedalnewconcept.adapter;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.activity.BabyHomeWorkActivity;
import com.ycsj.goldmedalnewconcept.activity.HomeWorkDetailActivity;
import com.ycsj.goldmedalnewconcept.bean.HomeWorkInfo;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.view.ImageInfo;
import com.ycsj.goldmedalnewconcept.view.ImagePreviewActivity;
import com.ycsj.goldmedalnewconcept.view.magicindicator.buildins.UIUtil;

/**
 * Created by Administrator on 2018/1/2 0002.
 */

public class BabyHomeworkAdapter extends CommonAdapter<HomeWorkInfo.ListBean> {
	private Context context;
	private List<String> showPicImgs;
	private int screenWidth;
	// 动态添加图片的宽高
	private int imgWidth;
	// 动态添加图片的布局参数
	private LinearLayout.LayoutParams lp;
	private Activity activity;
    
	// 查看大图的数据集合
	List<ImageInfo> imageInfo;
	private String role;

	public BabyHomeworkAdapter(Activity activity, Context context,
			List<HomeWorkInfo.ListBean> datas, int layoutId) {
		super(context, datas, layoutId);
		this.context = context;
		this.activity = activity;
		screenWidth = UIUtil.getScreenWidth(activity);
		imgWidth = (screenWidth - dip2px(context, (8 * 2 + 8 * 2 + 12 * 2))) / 3;
		lp = new LinearLayout.LayoutParams(imgWidth, imgWidth);
		
		role = SPUtils.getString(context, "role");
	}

	/**
	 * 创建查看大图的数据集合
	 */
	private void initDetailsImgs(List<String> showPicImgs) {
		imageInfo = new ArrayList<>();
		for (int i = 0; i < showPicImgs.size(); i++) {

			ImageInfo info = new ImageInfo();
			// 设置大图
			info.setBigImageUrl(showPicImgs.get(i));
			// 设置小图
			info.setThumbnailUrl(showPicImgs.get(i));
			imageInfo.add(info);
		}
	}

	@Override
	public void convert(ViewHolder holder, final HomeWorkInfo.ListBean info) {

		// 最外层的线性布局
		LinearLayout llOut = holder.getView(R.id.ll_out);
		llOut.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				
				if("2".equals(role)){
				
				Intent intent = new Intent(context,
						HomeWorkDetailActivity.class);
				intent.putExtra("userclass", info.getUser_class());
				intent.putExtra("rowid", info.getRow_id() + "");
				intent.putExtra("date", info.getCreate_date() + "");
				context.startActivity(intent);
				}
			}
		});

		// 时间
		TextView tvTime = holder.getView(R.id.tv_time);
		tvTime.setText(info.getCreate_date());

		// 上次作业评价
		TextView tvLastcontent = holder.getView(R.id.tv_lastcontent);
		tvLastcontent.setText(info.getComment());

		// 这次作业评价
		TextView tvThiscontent = holder.getView(R.id.tv_thiscontent);
		tvThiscontent.setText(info.getContent());

		// 用于展示图片的线性布局
		LinearLayout llPic = holder.getView(R.id.ll_showpic);
		
		LinearLayout ll_dianping =  holder.getView(R.id.ll_dianping);
		
		if(TextUtils.isEmpty(info.getComment())){
			ll_dianping.setVisibility(ll_dianping.GONE);
		}else{
			ll_dianping.setVisibility(ll_dianping.VISIBLE);
		}
		
		
		
		// 创建展示图片的数据集合
		final List<String> showPicImgs = new ArrayList<>();
		if (!TextUtils.isEmpty(info.getUser_picture1())) {
			showPicImgs.add(info.getUser_picture1());
		}

		if (!TextUtils.isEmpty(info.getUser_picture2())) {
			showPicImgs.add(info.getUser_picture2());
		}

		if (!TextUtils.isEmpty(info.getUser_picture3())) {
			showPicImgs.add(info.getUser_picture3());
		}
		if (!TextUtils.isEmpty(info.getUser_picture4())) {
			showPicImgs.add(info.getUser_picture4());
		}
		if (!TextUtils.isEmpty(info.getUser_picture5())) {
			showPicImgs.add(info.getUser_picture5());
		}
		if (!TextUtils.isEmpty(info.getUser_picture6())) {
			showPicImgs.add(info.getUser_picture6());
		}
		if (!TextUtils.isEmpty(info.getUser_picture7())) {
			showPicImgs.add(info.getUser_picture7());
		}
		if (!TextUtils.isEmpty(info.getUser_picture8())) {
			showPicImgs.add(info.getUser_picture8());
		}

		if (!TextUtils.isEmpty(info.getUser_picture9())) {
			showPicImgs.add(info.getUser_picture9());
		}

		// 配置线性布局的数据
		if (null == showPicImgs || showPicImgs.size() == 0) {
			// 证明没有数据
			llPic.setVisibility(View.GONE);
		} else {
			// 证明有数据
			llPic.setVisibility(View.VISIBLE);
			// 首先移除所有的子View
			if (llPic.getChildCount() != 0) {
				llPic.removeAllViews();
			}
			// 添加子View
			for (int i = 0; i < showPicImgs.size(); i++) {
				ImageView lv = new ImageView(context);
				lv.setTag(i);
				lv.setScaleType(ImageView.ScaleType.CENTER_CROP);
				Picasso.with(context).load(showPicImgs.get(i)).into(lv);
				if (i == 0) {
					lp.leftMargin = 0;
				} else {
					lp.rightMargin = dip2px(context, 12);
				}

				llPic.addView(lv, lp);

				// 图片的点击事件
				lv.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View view) {

						if (null == showPicImgs || showPicImgs.size() == 0) {
							return;
						}

						// 初始化查看大图的数据集合
						initDetailsImgs(showPicImgs);

						// 跳转到查看大图的界面
						Intent intent = new Intent(context,
								ImagePreviewActivity.class);
						Bundle bundle = new Bundle();
						bundle.putSerializable(ImagePreviewActivity.IMAGE_INFO,
								(Serializable) imageInfo);
						bundle.putInt(ImagePreviewActivity.CURRENT_ITEM,
								(int) view.getTag());
						intent.putExtras(bundle);
						context.startActivity(intent);
					}
				});
			}
		}
	}

	/**
	 * dp转px的方法
	 *
	 * @param context
	 * @param dpValue
	 * @return
	 */
	public int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
}
